# THIS SCRIPT COMPUTES SUMMARY STATISTICS FOR FIGURE 6, AND PROVIDES
# THE SYNTAX FOR CREATING THE FIGURE ITSELF. IT MUST BE RUN IN CONJUNCTION 
# WITH THE DATASET "ANESCumulative_cleaned.csv", AVAILABLE IN THE REPLICATION 
# MATERIALS ON DATAVERSE. 
rm(list = ls())
library(foreign)
library(car)
library(readstata13)
library(ggplot2)
library(stargazer)
library(grid)
library(gridExtra)
library(survey)
library(dplyr)
library(lemon)
library(ggpubr)
library(reshape2)

# READ IN THE DATA. SELECT THE FILE "ANESCumulative_cleaned.csv"
our.data = read.csv(file.choose())
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# % OPPOSE CUTTING SPENDING ON SOCIAL SECURITY

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# PROPORTIONS, REPUBLICANS
our.means = svytable(~ pm_ss + programmatic + year, design = subset(our.design, republican==1))
republicans.props = prop.table(our.means, 2:3)
republicans.props = as.data.frame(republicans.props)
# PROPORTIONS, DEMOCRATS
our.means = svytable(~ pm_ss + programmatic + year, design = subset(our.design, democrat==1))
democrats.props = prop.table(our.means, 2:3)
democrats.props = as.data.frame(democrats.props)
# BIND THE TWO DATASETS
proportions.data = rbind(republicans.props, democrats.props)
proportions.data = subset(proportions.data, pm_ss==1)
proportions.data$partisanship = c(rep("Republicans", 39), 
                                  rep("Democrats", 39))
# YEAR
proportions.data$year = as.numeric(as.character(proportions.data$year))
# PROGRAMMATIC
proportions.data$programmatic = car::recode(proportions.data$programmatic,
                                            "0 = 'Unmatched';
                                            0.5 = 'Partially\n matched';
                                            1 = 'Matched'; else = NA")
proportions.data$item = rep("Social Security", length(proportions.data$pm_ss))
data.pm_ss = proportions.data
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# % OPPOSE CUTTING SPENDING ON CHILDCARE

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# PROPORTIONS, REPUBLICANS
our.means = svytable(~ pm_childcare + programmatic + year, design = subset(our.design, republican==1))
republicans.props = prop.table(our.means, 2:3)
republicans.props = as.data.frame(republicans.props)
# PROPORTIONS, DEMOCRATS
our.means = svytable(~ pm_childcare + programmatic + year, design = subset(our.design, democrat==1))
democrats.props = prop.table(our.means, 2:3)
democrats.props = as.data.frame(democrats.props)
# BIND THE TWO DATASETS
proportions.data = rbind(republicans.props, democrats.props)
proportions.data = subset(proportions.data, pm_childcare==1)
proportions.data$partisanship = c(rep("Republicans", 30), 
                                  rep("Democrats", 30))
# YEAR
proportions.data$year = as.numeric(as.character(proportions.data$year))
# PROGRAMMATIC
proportions.data$programmatic = car::recode(proportions.data$programmatic,
                                            "0 = 'Unmatched';
                                            0.5 = 'Partially\n matched';
                                            1 = 'Matched'; else = NA")
proportions.data$item = rep("Childcare", length(proportions.data$pm_childcare))
data.pm_childcare = proportions.data
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# % OPPOSE CUTTING SPENDING ON CRIME

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# PROPORTIONS, REPUBLICANS
our.means = svytable(~ pm_crime + programmatic + year, design = subset(our.design, republican==1))
republicans.props = prop.table(our.means, 2:3)
republicans.props = as.data.frame(republicans.props)
# PROPORTIONS, DEMOCRATS
our.means = svytable(~ pm_crime + programmatic + year, design = subset(our.design, democrat==1))
democrats.props = prop.table(our.means, 2:3)
democrats.props = as.data.frame(democrats.props)
# BIND THE TWO DATASETS
proportions.data = rbind(republicans.props, democrats.props)
proportions.data = subset(proportions.data, pm_crime==1)
proportions.data$partisanship = c(rep("Republicans", 30), 
                                  rep("Democrats", 30))
# YEAR
proportions.data$year = as.numeric(as.character(proportions.data$year))
# PROGRAMMATIC
proportions.data$programmatic = car::recode(proportions.data$programmatic,
                                            "0 = 'Unmatched';
                                            0.5 = 'Partially\n matched';
                                            1 = 'Matched'; else = NA")
proportions.data$item = rep("Dealing with crime", length(proportions.data$pm_crime))
data.pm_crime = proportions.data
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# MERGE 
new.data = rbind(data.pm_ss[-c(1)], data.pm_childcare[-c(1)], data.pm_crime[-c(1)])
levels(new.data$programmatic) = c("Matched", "Partially \nmatched", "Unmatched")

# FIGURE 6: PROPORTION THAT OPPOSE DECREASED SPENDING ON FEDERAL PROGRAMS, 
# 1984-2020, BY PARTISANSHIP AND MATCHING STATUS. 
our.plot = ggplot(new.data,
                  aes(year, Freq, group = partisanship, 
                      linetype =  partisanship)) +
  geom_line(aes(linetype = partisanship), linewidth = 1) +
  labs(linetype = "Partisanship") + 
  xlab("Year") +
  ylab("Proportion \n") +
  # ggtitle("Proportion that Oppose Decreased Spending on Federal Programs, \n1984-2020, By Partisanship and Matching Status", 
  #        subtitle = "Data: ANES cumulative file, partisan identifiers only. Dashed line indicates 50 percent. \nPlots indicate the proportion of respondents that said spending should be increased or kept the same.") +  
  # theme_bw() +  
  theme_minimal() +
  # scale_color_grey() +
  facet_grid(programmatic ~ item, scales = "free_x") +
  theme(legend.position = "none") +
  theme(axis.title.x = element_text(size = 9)) + 
  theme(axis.title.y = element_text(size = 9)) +
  # theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 14)) + 
  # theme(plot.subtitle = element_text(hjust = 0.5, size = 12)) +
  theme(strip.text = element_text(face = "bold", size = 9)) + ylim(0, 1) +
  geom_hline(yintercept = 0.5, linetype = "dashed") + 
  theme(panel.spacing = unit(1, "lines"))
our.plot

# SAVE
# ggsave(our.plot, file = "Schmidtetal-Figure6.pdf", width = 6.6, height = 9)
